package com.xinheyun.demo.leetcode;

/**
 * @description: TODO
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/9/8 11:54
 * @version:v1.0
 */

//2 - two
//    3 - three
//    4 - four
//    5 - five
//    6 - six
//    7 - seven
//    8 - eight
//    9 - nine
//    10 - ten
//    11 - eleven
//    12 - twelve
//    13 - thirteen
//    14 - fourteen
//    15 -
public class test273 {

    public static void main(String[] args) {
        test273 test273 = new test273();
//        for(int i =1;i<=1000010000;i=i*10) {
        System.out.println("sss" + test273.numberToWords(1100000000) + "lll");
//        }
    }

    public String numberToWords(int num) {
        if (num == 0) {
            return "Zero";
        }
        String[] nums = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
            "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"};
        String[] tenDigit = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", "Hundred"};
        String[] unit = {"", "Thousand", "Million", "Billion"};

        boolean flag1 = true;
        StringBuilder stringBuilder = new StringBuilder();
        int length = Integer.toString(num).length();
        boolean isOne = false;
        boolean flag = false;
        for (int i = length; i > 0; i--) {
            int number = Integer.toString(num).charAt(length - i) - 48;
            int FirstLength = i % 3;

            if (flag && number != 0 && !isOne) {
                stringBuilder.append(" ");
            }
            if (FirstLength == 2) {
                if (number == 1) {
                    isOne = true;
                    continue;
                }
                stringBuilder.append(tenDigit[number]);
                if (number != 0) {
                    flag1 = true;
                }
            }
            if (FirstLength == 1) {
                if (isOne) {
                    number = number + 10;
                    isOne = false;
                }
                stringBuilder.append(nums[number]);
                if (i / 3 > 0 && (number != 0 || flag1)) {
                    stringBuilder.append(" ");
                    stringBuilder.append(unit[i / 3]);
                    flag1 = false;
                }
            }
            if (FirstLength == 0) {
                if (number != 0) {
                    stringBuilder.append(nums[number]);
                    stringBuilder.append(" ");
                    stringBuilder.append(tenDigit[10]);
                }
                if (number != 0) {
                    flag1 = true;
                }
            }
            flag = true;
        }

        return stringBuilder.toString();
    }
}
